package cheqex.update.controllers;

import javax.swing.JOptionPane;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.*;

public class DateController {
	private static int yyyy;
	private static int mm;
	private static int dd;
	private static int y;
	private static int m;
	private static int d;
	private static String date;
	private static java.sql.Date sqlDate;
	
	public DateController(int year, int month, int day){
		this.yyyy = year;
		this.mm = month;
		this.dd = day;
	}
	
	
	public static boolean checkError(int yyyy, int mm, int dd){
		//for months January, March, April
		if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12){
			if(dd>31){
				JOptionPane.showMessageDialog(null, "Date should be before or until 31", "Error", JOptionPane.WARNING_MESSAGE);
				return false;
			}
			else
				return true;
		}
		else if(mm==2){
			if((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0)){
				if(dd>29){
					JOptionPane.showMessageDialog(null, "Date should be before or until 29", "Error", JOptionPane.WARNING_MESSAGE);
					return false;
				}
				else
					return true;
			}
			else{
				if(dd>28){
					JOptionPane.showMessageDialog(null, "Date should be before or until 28", "Error", JOptionPane.WARNING_MESSAGE);
					return false;
				}
				else
					return true;
			}
		}
		else if(mm==4||mm==6||mm==9||mm==11){
			if(dd>30){
				JOptionPane.showMessageDialog(null, "Date should be before or until 30", "Error", JOptionPane.WARNING_MESSAGE);
				return false;
			}
			else
				return true;
		}
		return true;	
	}
	
	public static String saveDate(int yyyy, int mm, int dd){
		try{
			if(checkError(yyyy, mm, dd)){
				saveYear(yyyy);
				saveMonth(mm);
				saveDay(dd);
				date = yyyy+"-"+mm+"-"+dd;
				return date;
			}else{
				return null;
			}
		}catch (Exception e) { return null; }
	}
	
	public static java.sql.Date getDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date utilDate;
		try {
			if(date!=null){
				utilDate = sdf.parse(date);
				sqlDate = new java.sql.Date(utilDate.getTime());
				return sqlDate;
			}else{
				utilDate = new java.util.Date();
				return sqlDate = new java.sql.Date(utilDate.getTime());
			}
		} catch (ParseException e) {
			e.printStackTrace();
			utilDate = new java.util.Date();
			return sqlDate = new java.sql.Date(utilDate.getTime());
		}
		
	
	}
	
	public static void saveYear(int year){
		y = year;
	}
	
	public static void saveMonth(int month){
		m = month;
	}
	
	public static void saveDay(int day){
		d = day;
	}
	
	public static int getYear(){
		return y;
	}
	
	public static int getMonth(){
		return m;
	}
	
	public static int getDay(){
		return d;
	}
	
	public static java.sql.Date getToday(){
		String current=null;
		Calendar c = Calendar.getInstance();
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DATE);
		int y = c.get(Calendar.YEAR);
		current = ""+ m + d + y;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date utilDate;
		java.sql.Date sDate;
		try {
			if(current!=null){
				utilDate = sdf.parse(current);
				sDate = new java.sql.Date(utilDate.getTime());
				return sDate;
			}else{
				utilDate = new java.util.Date();
				return sDate = new java.sql.Date(utilDate.getTime());
			}
		} catch (ParseException e) {
			e.printStackTrace();
			utilDate = new java.util.Date();
			return sDate = new java.sql.Date(utilDate.getTime());
		}
	}
}